package com.gg.user.controller;

import base.BaseServlet;
import com.gg.beanhelper.BeanHelper;
import com.gg.entity.User;
import com.gg.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/AddUserServlet")
public class AddUserServlet extends BaseServlet {
    @Autowired
    private UserService userService;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = BeanHelper.getBean(request.getParameterMap(), User.class);
        boolean res = userService.regist(user);
        response.setContentType("text/html;charset=utf-8");
        if (res) {
            response.getWriter().print("<script>alert('添加成功！！！');location.href='/fruits/ShowUsersServlet?pageNum=1'</script>");
        } else {
            response.getWriter().print("<script>alert('添加失败！！！');location.href='/fruits/admin/BSindex2.jsp'</script>");
        }
    }
}
